package fr.alma.domo.svc;


import java.util.ArrayList;
import java.util.List;

import org.oasisopen.sca.annotation.AllowsPassByReference;
import org.oasisopen.sca.annotation.Service;

import fr.alma.domo.model.AbstractService;
import fr.alma.domo.model.Description;
import fr.alma.domo.model.Resource;

/**
 * 
 * @author steg
 *
 */
@SuppressWarnings("serial")
@Service(SelectionAndDiscovery.class)
@AllowsPassByReference
public class SelectionAndDiscoveryImpl extends AbstractService implements SelectionAndDiscovery {

	public SelectionAndDiscoveryImpl(){
		super("SelectionAndDiscovery");
	}

	@Override
	public Resource select(Description description) {
		for(List<Resource> list : Resource.getRegistry().values()){
			for(Resource r : list){				
				if(r.matches(description)){
					return r;
				}
			}
		}
		return null;
	}

	@Override
	public Resource selectByFamily(String family,Description description) {		
		List<Resource> list = Resource.getRegistry().get(family);
		if(list!=null){
			for(Resource r : list){				
				if(r.matches(description)){
					return r;
				}
			}
		}
		return null;
	}

	@Override
	public List<Resource> selectAll(Description description) {
		List<Resource> res = new ArrayList<Resource>();
		for(List<Resource> list : Resource.getRegistry().values()){
			for(Resource r : list){				
				if(r.matches(description)){
					res.add(r);
				}
			}
		}
		return res;
	}

	@Override
	public List<Resource> selectAllByFamily(String family, Description description) {
		List<Resource> res = new ArrayList<Resource>();
		List<Resource> list = Resource.getRegistry().get(family);
		if(list!=null){
			for(Resource r : list){				
				if(r.matches(description)){
					res.add(r);
				}
			}
		}
		return res;
	}
	
}
